export function Methods () {
    this.routers = {};
    this.path = "";
};

Methods.prototype = {

    init:function() {
        let self = this;
        /*
        * 监听URL是否发送变化
        * */
        window.onhashchange = function () {
            self.changeUrl();
        };
        window.onload = function () {
            self.changeUrl();
        };
    },
    /*
    * 参数1：url地址
    * 参数2：回调函数，内容是asyncFun函数
    * 参数3：组件
    * 参数4：组件参数
    * */
    map:function(path,callback,component,data){
        let self = this;
        if(callback&&typeof callback === "function"){
            self.routers[path] = {
                callback:callback,
                component:component,
                fn:null,
                data:data
            }
        }else{
            console.log("参数有问题");
        }
    },

    /*
    * url发生变化执行该行数
    * */
    changeUrl:function(){
        let self = this;
        let path = location.hash.split("#")[1];
        if(this.routers[path]){
            self.path = path;
            /*
            * 发生变化执行回调函数（asyncFun）
            * */
            self.routers[path].callback();
        }else{
            location.hash = "/index";
        }
    },

    /*
    * 异步加载，执行渲染文本
    * callback参数:导入的js文件函数
    * */
    asyncFun:function(callback) {
        let self = this;
        let routers = self.routers[self.path];
        if(routers.fn){
            routers.fn = routers.component?
                new callback(routers.component(routers.data)):
                routers.fn = new callback(routers.component);
        }else{
            routers.fn = routers.component?
                new callback(routers.component(routers.data)):
                routers.fn = new callback(routers.component);
        }
    }
};

